package com.cgw.game.adapter;

import android.graphics.Color;

import androidx.annotation.Nullable;

import com.cgw.game.R;
import com.cgw.game.entity.DoubleLineBar2Entity;
import com.cgw.game.entity.SleepStatusData;
import com.cgw.game.view.CustomCombinedChart;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

/**
 * @类描述 describe
 * @作者 YF
 * @创建时间 2019/12/31 16:13
 */
public class DoubleLineBar2Adapter extends BaseQuickAdapter<DoubleLineBar2Entity, BaseViewHolder> {
    @Override
    protected void convert(BaseViewHolder helper, DoubleLineBar2Entity item) {
        CustomCombinedChart chart = helper.getView(R.id.chart);
        initBarLineChart(chart);
        setDate(chart,item);
    }

    public DoubleLineBar2Adapter(@Nullable List<DoubleLineBar2Entity> data) {
        super(R.layout.double_line_bar2_item,data);
    }

    private void initBarLineChart(CustomCombinedChart chart) {
        chart.setTouchEnabled(false);
        chart.setPinchZoom(false);
        chart.setScaleYEnabled(false);
        chart.setDragEnabled(true);
        chart.setDoubleTapToZoomEnabled(false);
        //取消说明
        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);
        chart.setNoDataText("暂无数据");

        chart.setExtraOffsets(-5,-5,-5,-5);


        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTextSize(10f);
        leftAxis.setTextColor(Color.parseColor("#91a2a3"));
        leftAxis.setGridColor(Color.parseColor("#e3edf2"));
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(200f);
        leftAxis.setLabelCount(5, true);
        leftAxis.setEnabled(false);



        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setTextColor(Color.RED);
        rightAxis.setAxisMinimum(0);
        rightAxis.setDrawGridLines(false);
        rightAxis.setGranularityEnabled(false);
        rightAxis.setEnabled(false);

        //横向轴线设置
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12f);
        xAxis.setTextColor(Color.parseColor("#91a2a3"));
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(287f);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularityEnabled(true);
        //设置为虚线
        xAxis.setDrawAxisLine(true);
        xAxis.setEnabled(false);  //是否打开x轴
    }

    private void setDate(CustomCombinedChart chart,DoubleLineBar2Entity item) {
        chart.clear();

        LineData lineData = new LineData(getHeartEntry(item.getHeart()),getStaminaEntry(item.getMotion()));
        CombinedData data = new CombinedData();
        data.setData(lineData);

        BarData barData = new BarData();
        barData.addDataSet(getBarEntry(item.getSleep()));// 添加一组柱形图，如果有多组柱形图数据，则可以多次addDataSet来设置
        barData.setBarWidth(1f);

        data.setData(barData);


        chart.setData(data);
        chart.notifyDataSetChanged();
        chart.invalidate();

    }

    private LineDataSet getHeartEntry(List<Float> heart){
        List<Entry> values = new ArrayList<>();
        for (int i = 0; i < heart.size(); i++) {
            values.add(new Entry(i,heart.get(i)));
        }
        LineDataSet lineDataSet;
        lineDataSet = new LineDataSet(values, "心率");
        lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSet.setColor(mContext.getResources().getColor(R.color.red));
        lineDataSet.setHighlightEnabled(false);
        lineDataSet.setDrawFilled(false);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawCircles(false);
        lineDataSet.setDrawValues(false);
        return lineDataSet;
    }

    private LineDataSet getStaminaEntry(List<Float> stamina){
        List<Entry> values = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < stamina.size(); i++) {
            values.add(new Entry(i*12,stamina.get(i)));

        }
        LineDataSet lineDataSet = new LineDataSet(values, "体能");
        lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        lineDataSet.setColor(Color.TRANSPARENT);
        lineDataSet.setCircleColor(mContext.getResources().getColor(R.color.orange_5));
        lineDataSet.setHighlightEnabled(false);
        lineDataSet.setDrawFilled(false);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(5f);
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawCircles(true);
        lineDataSet.setDrawValues(false);
        return lineDataSet;
    }

    private BarDataSet getBarEntry(List<SleepStatusData> sleepStatus){
        List<BarEntry> values = new ArrayList<>();
        int j =0;
        for (int i = 0; i < sleepStatus.size(); i++) {
            int duration = sleepStatus.get(i).getDuration();
            switch (sleepStatus.get(i).getStatus()) {
                case "LSSleepStatusAwake":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 240, sleepStatus.get(i))));
                        j++;
                    }
                    break;
                case "LSSleepStatusLight":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 230, sleepStatus.get(i))));
                        j++;
                    }
                    break;
                case "LSSleepStatusDeep":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 225, sleepStatus.get(i))));
                        j++;
                    }
                    break;
                case "LSSleepStatusSober":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 2, sleepStatus.get(i))));
                        j++;
                    }
                    break;
                case "LSSleepStatusNap":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 250, sleepStatus.get(i))));
                        j++;
                    }
                    break;
                case "LSSleepStatusWorn":
                    for (int i1 = 0; i1 < duration / 5; i1++) {
                        values.add((new BarEntry(j, 220, sleepStatus.get(i))));
                        j++;
                    }
                    break;
            }
        }
        BarDataSet barDataSet = new BarDataSet(values, "睡眠"); // 新建一组柱形图，"LAR"为本组柱形图的Label
        barDataSet.setColor(mContext.getResources().getColor(R.color.blue_21)); // 设置柱形图颜色
        //不显示值
        barDataSet.setDrawValues(false);
        barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        barDataSet.setBarBorderWidth(1f);
        barDataSet.setHighlightEnabled(false);
        return barDataSet;
    }
}
